//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.review
//
//import com.intellij.collaboration.async.computationState
//import com.intellij.collaboration.async.stateInNow
//import com.intellij.collaboration.util.ComputedResult
//import com.intellij.openapi.actionSystem.DataKey
//import com.intellij.openapi.diagnostic.logger
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.util.io.await
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.cancel
//import kotlinx.coroutines.currentCoroutineContext
//import kotlinx.coroutines.flow.StateFlow
//import kotlinx.coroutines.launch
//import com.linqingying.gitcode.pullrequest.data.GCPullRequestPendingReview
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRDataProvider
//import com.linqingying.gitcode.pullrequest.data.provider.createPendingReviewRequestsFlow
//
//interface GCPRReviewViewModel {
//  /**
//   * Pending pull request review for a current user
//   */
//  val pendingReview: StateFlow<ComputedResult<GCPullRequestPendingReview?>>
//
//  /**
//   * UI handler for review submission request
//   */
//  var submitReviewInputHandler: (suspend (GCPRSubmitReviewViewModel) -> Unit)?
//
//  /**
//   * Request review submission
//   *
//   * [submitReviewInputHandler] has to be set before invoking this method
//   */
//  fun submitReview()
//
//  companion object {
//    val DATA_KEY: DataKey<GCPRReviewViewModel> = DataKey.create("GitCode.PullRequests.Review.ViewModel")
//  }
//}
//
//internal class DelegatingGCPRReviewViewModel(private val helper: GCPRReviewViewModelHelper) : GCPRReviewViewModel {
//  override val pendingReview: StateFlow<ComputedResult<GCPullRequestPendingReview?>> = helper.pendingReviewState
//  override var submitReviewInputHandler: (suspend (GCPRSubmitReviewViewModel) -> Unit)? = null
//
//  override fun submitReview() {
//    val handler = submitReviewInputHandler
//    checkNotNull(handler) { "UI handler was not set" }
//    helper.submitReview(handler)
//  }
//}
//
//internal class GCPRReviewViewModelHelper(parentCs: CoroutineScope, private val dataProvider: GCPRDataProvider) {
//  private val cs = parentCs.childScope()
//  private val reviewData = dataProvider.reviewData
//
//  val pendingReviewState: StateFlow<ComputedResult<GCPullRequestPendingReview?>> =
//    reviewData.createPendingReviewRequestsFlow().computationState().stateInNow(cs, ComputedResult.loading())
//
//  fun submitReview(handler: (suspend (GCPRSubmitReviewViewModel) -> Unit)) {
//    val pendingReviewResult = pendingReviewState.value.result ?: return
//    cs.launch {
//      val ctx = currentCoroutineContext()
//      val viewerIsAuthor = try {
//        dataProvider.detailsData.loadDetails().await().viewerDidAuthor
//      }
//      catch (e: Exception) {
//        LOG.info("Details loading failed", e)
//        return@launch
//      }
//      val vm = GCPRSubmitReviewViewModelImpl(this, reviewData, viewerIsAuthor, pendingReviewResult.getOrNull()) {
//        ctx.cancel()
//      }
//      handler.invoke(vm)
//    }
//  }
//
//  init {
//    reviewData.resetPendingReview()
//  }
//
//  companion object {
//    private val LOG = logger<GCPRReviewViewModelHelper>()
//  }
//}
